package com.hankai.controller.backend;

import com.hankai.common.Const;
import com.hankai.common.ResponseCode;
import com.hankai.common.ServerResponse;
import com.hankai.pojo.User;
import com.hankai.service.ICategoryService;
import com.hankai.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Created by hankai on 2017/6/7.
 */
@Controller
@RequestMapping("manage/category/")
public class CategoryManageController {
    @Resource
    private IUserService iUserService;
    @Resource
    private ICategoryService iCategoryService;

    /**
     * 添加分类节点
     * @param session
     * @param categoryName
     * @param parentId
     * @return
     */
    @RequestMapping(value = "addCategory.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse addCategory(HttpSession session,String categoryName,@RequestParam(value = "parentId",defaultValue = "0") Integer parentId){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if(currentUser==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),"请登录");
        }
        ServerResponse response = iUserService.checkIsNotAdmin(currentUser);
        if(!response.isSuccess()){
            return response;
        }
        response = iCategoryService.addCategory(categoryName,parentId);
        return response;
    }

    /**
     * 修改节点
     * @param session
     * @param categoryName
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "updateCategory.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updateCategory(HttpSession session,String categoryName,Integer categoryId){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if(currentUser==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),"请登录");
        }
        ServerResponse response = iUserService.checkIsNotAdmin(currentUser);
        if(!response.isSuccess()){
            return response;
        }
        return  iCategoryService.updateCategory(categoryName,categoryId);
    }

    /**
     * 根据id查询子节点
     * @param session
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "getChildenParallelCategory.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getChildenParallelCategory(HttpSession session,@RequestParam(value = "parentId",defaultValue = "0") Integer parentId){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if(currentUser==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),"请登录");
        }
        ServerResponse response = iUserService.checkIsNotAdmin(currentUser);
        if(!response.isSuccess()){
            return response;
        }
        return iCategoryService.getChildenParallelCategory(parentId);
    }

    /**
     * 根据id查询子节点(递归获取节点下的节点，如果下面还有，继续获取)
     * @param session
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "getChildenAndDeepParallelCategory.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getChildenAndDeepParallelCategory(HttpSession session,@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if(currentUser==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),"请登录");
        }
        ServerResponse response = iUserService.checkIsNotAdmin(currentUser);
        if(!response.isSuccess()){
            return response;
        }
        return iCategoryService.getChildenAndDeepParallelCategory(categoryId);
    }
}
